package com.sykj.bsc.hyx.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.bsc.hyx.bean.Bis_wechat_record;
import com.sykj.bsc.hyx.service.Bis_wechat_recordService;
import com.sykj.bsc.util.Tools;
import com.sykj.bsc.util.WechatUtil;
import com.sykj.bsc.zjj.bean.Bis_custom_user;

@Controller
@RequestMapping("bis_wechat_record")//起的名字
public class Bis_wechat_recordController {
/*
 * 微信
 */
	@Autowired//全部扫描
	private Bis_wechat_recordService bis_wechat_recordService;
	
	@RequestMapping("/selectbis_wechat_record")//显示起的名字
	public String selectbis_wechat_record(Model model,HttpServletRequest request,@RequestParam(value="page",defaultValue="1")int page){
		Map map=initMap(request);
		PageHelper.startPage(page, 5);
		List<Bis_wechat_record> Bis_wechat_record=bis_wechat_recordService.selectBis_wechat_record(map);
		PageInfo<Bis_wechat_record> pageInfo=new PageInfo<Bis_wechat_record>(Bis_wechat_record);
		model.addAttribute("Bis_wechat_record", Bis_wechat_record);
		model.addAttribute("p", pageInfo);
		return "backstage/hyx/selectbis_wechat_record";
	}

	private Map initMap(HttpServletRequest request) {
		String record_id=request.getParameter("record_id");//收件人
		String send_user_id=request.getParameter("send_user_id");//收件人账号
		String content=request.getParameter("content");//内容
		String send_time1=request.getParameter("send_time1");//发起时间1
		String send_time2=request.getParameter("send_time2");//发起时间2
	    Map<String, Object> map=new HashMap<String,Object>();//map集合存值
		map.put("record_id",record_id );//收件人
		map.put("send_user_id",send_user_id );//收件人账号
		map.put("content",content );//内容
		map.put("send_time1",send_time1 );//发起时间1
		map.put("send_time2",send_time2 );//发起时间2
		return map;
	}
	//全选和单选删除
	@RequestMapping("delete")
	public String delete(String ids){
		bis_wechat_recordService.delete(ids);
		System.out.println("删除id:"+ids);
		return "redirect:/bis_wechat_record/selectbis_wechat_record";
	}
	//跳到系统发消息
	@RequestMapping("list")
	public String list(){
		
		return "backstage/hyx/saveBis_wechat_record";
	}
	//从系统发消息跳到显示顾客表
	@RequestMapping("list2")
	public String list2(Model model,HttpServletRequest request){
		String mobile=request.getParameter("mobile");
		String username=request.getParameter("username");
		System.out.println("mobile:"+mobile+",username:"+username);
		Map map=new HashMap<>();
		map.put("mobile", mobile);
		map.put("username", username);
		List<Bis_custom_user> list=bis_wechat_recordService.selectBis_custom_user(map);
		model.addAttribute("list", list);
		return "/backstage/hyx/ListBis_custom_user";
	}
	//根据顾客表id查询  在
	@RequestMapping("select")
	public String select(String custom_user_id1,Model model){
		 String[] data = custom_user_id1.split(",");
		 String openids="";
		for (String  custom_user:data) {
		      Bis_custom_user bis_custom_user= bis_wechat_recordService.getById(custom_user);
		      openids+=bis_custom_user.getOpenid()+",";
			}
		if(openids.length()>0){
			openids=openids.substring(0,openids.length()-1);
		}
		model.addAttribute("openids",openids);
		model.addAttribute("userids",custom_user_id1);
		return "backstage/hyx/saveBis_wechat_record";
	}
	
	//发到信息表里面
	@RequestMapping("saveBis_wechat_record")
	public String saveBis_wechat_record(HttpServletRequest request,String openid1){
		String send_user_id=request.getParameter("custom_user_id");
		String mobile=request.getParameter("mobile");//手机号
		String content=request.getParameter("content");//内容
		String[] openid2=openid1.split(",");//去掉，
		List<String> list=new ArrayList<String>();//集合
		for (String open:openid2) {//遍历数组循环取值
			if(Tools.isNotNull(open)){
				list.add(open);
			}
		}
		WechatUtil.sendTextMessage(list, content);
		// 获取开发者账号信息
		  /*  Accountlnfo.execute();
			// 验证码通知短信接口   手机号和发送短信的内容
			 com.sykj.bsc.hyx.controller.miaodiyun.httpApiDemo.common.common.IndustrySMS.execute(mobile, content);*/
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("send_user_id",send_user_id);
		map.put("mobile",mobile);
		map.put("content",content);
		/*map.put("openid", openid);*/
		this.bis_wechat_recordService.saveBis_wechat_record(map);
		return "redirect:/bis_wechat_record/selectbis_wechat_record";
	}
}
